
-- local t = { g="male", age="36", name="dorong", hp="010333333"}
-- for i, v in pairs(display) do
	-- print(i, v)
-- end

-- print(display.contentHeight)
-- print(display["contentHeight"])

local physics = require("physics")
local function setupPhysics()
	physics.start()
	physics.setGravity(0,0)
end

local function createBall()
	local ball = display.newCircle(display.contentWidth / 2, display.contentHeight / 2, 10)

	physics.addBody(ball, "dynamic", {friction=0, bounce=1, radius=10})
	ball:setLinearVelocity(75, 150)
end

local function createWall(wallThickness)
	local wall = display.newRect(0, 0, display.contentWidth, wallThickness)
	local wall2 = display.newRect(display.contentWidth - wallThickness, wallThickness, wallThickness, display.contentHeight - wallThickness)
	local wall3 = display.newRect(0, wallThickness, wallThickness, display.contentHeight - wallThickness)
	local wall4 = display.newRect(wallThickness, display.contentHeight - wallThickness, display.contentWidth - wallThickness * 2, wallThickness)
	physics.addBody(wall, "static")
	physics.addBody(wall2, "static")
	physics.addBody(wall3, "static")
	physics.addBody(wall4, "static")
	wall4:addEventListener("collision", function(event)
		if "began" == event.phase then
			event.other:removeSelf()
			Runtime:dispatchEvent({name="gameEnded"})
			timer.performWithDelay(500, function(event) createBall() end, 1)
		end
	end)
	wall4.isVisible = false
end

local function createPaddle()
	local paddleWidth = 100
	local paddle = display.newRect(display.contentWidth / 2 - paddleWidth / 2, display.contentHeight - 50, paddleWidth, 20)
	Runtime:addEventListener("touch", function(event)
		paddle.x = event.x
	end)
	physics.addBody(paddle, "static")
	local function makeBallFaster(event)
		local ball = event.other
		local x, y = ball:getLinearVelocity()
		ball:setLinearVelocity(x * 1.2, y * 1.2)
	end
	paddle:addEventListener("postCollision", makeBallFaster)
end

local function createBricks()
	local brickWidth = 50
	local brickXCount = 9
	local brickHeight = 20
	local brickYCount = 10
	local col
	local row
	for row = 1, brickYCount do
		for col = 1, brickXCount do
			local brick = display.newRect(col * brickWidth - 35, row * brickHeight, brickWidth, brickHeight)
			brick:setFillColor(math.random(0, 255), math.random(0, 255) , math.random(0, 255))
			physics.addBody(brick, "static")
			brick:addEventListener("collision", function(event)
				if event.phase == "ended" then
					event.target:removeSelf()
					Runtime:dispatchEvent({name="brickBroken"})
				end
			end)
		end
	end
end

local function createScore()
	local fontSize = 32
	local score = display.newText("0", display.contentWidth - 100, display.contentHeight - fontSize, native.systemFont, fontSize)
	score:setTextColor(255,255,255)
	score:setReferencePoint(display.BottomRightReferencePoint)
	score.x = display.contentWidth - 20
	function score.setScore(value)
		score.text = value
		Runtime:dispatchEvent({name="scoreChanged", score=value})
		score:setReferencePoint(display.BottomRightReferencePoint)
		score.x = display.contentWidth - 20
	end
	Runtime:addEventListener("brickBroken", function(event)
		score.setScore(120 + score.text)
	end)
	Runtime:addEventListener("gameEnded", function(event)
		score.setScore(0)
	end)
end

local function createHighscore()
	local fontSize = 32
	local score = display.newText("0", 110, display.contentHeight - fontSize, native.systemFont, fontSize)
	score:setTextColor(255, 255, 255)
	Runtime:addEventListener("scoreChanged", function(event)
		if event.score > tonumber(score.text) then
			score.text = event.score
		end
	end)
end

local function main()
	setupPhysics()
	createBall()
	createWall(10)
	createPaddle()
	createBricks()
	createScore()
	createHighscore()
end

main()